package org.academic.texteditor.gui.util;

import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.JComponent;

public class ComponentUtils {

	public static final int NORTH = 1;
	public static final int NORTH_WITHOUT_INTERVAL = 2;
	public static final int WEST = 3;
	public static final int WEST_WITHOUT_INTERVAL = 4;
	public static final int EAST = 5;
	public static final int EAST_WITHOUT_INTERVAL = 6;
	public static final int SOUTH = 7;
	public static final int SOUTH_WITHOUT_INTERVAL = 8;
	public static final int NORTHEAST = 9;
	public static final int NORTHEAST_WITHOUT_INTERVAL = 10;

	public static final int LEFT = 1;
	public static final int CENTER = 2;
	public static final int RIGHT = 3;
	
	public static int xInterval = 10;
	public static int yInterval = 10;
	
	public static void position(JComponent component, int position, JComponent reference, int width, int height) {
		Rectangle referencePosition = reference.getBounds();
		
		Rectangle newPosition = new Rectangle();
		newPosition.setSize(width, height);
		
		Point point = componentPosition(position, width, height, referencePosition);
		newPosition.setLocation(point);
		component.setBounds(newPosition);
	}
	
	private static Point componentPosition(int position, int width, int height, Rectangle referencePosition) {
	    Point point = new Point();
		switch (position) {
	        case NORTH:
	        	point.y = (int) referencePosition.getY() - (height + yInterval);
	        	point.x = (int) referencePosition.getX();
		        break;
	        case NORTH_WITHOUT_INTERVAL:
	        	point.y = (int) referencePosition.getY() - height;
	        	point.x = (int) referencePosition.getX();
		        break;
	        case WEST:
	        	point.y = (int) referencePosition.getY();
	        	point.y = (int) referencePosition.getX() - (width + xInterval);
	        	break;
	        case WEST_WITHOUT_INTERVAL:
	        	point.y = (int) referencePosition.getY();
	        	point.y = (int) referencePosition.getX() - width;
	        	break;
	        case EAST:
	        	point.y = (int) referencePosition.getY();
	        	point.x = (int) (referencePosition.getX() + referencePosition.getWidth() + xInterval);
	        	break;
	        case EAST_WITHOUT_INTERVAL:
	        	point.y = (int) referencePosition.getY();
	        	point.x = (int) (referencePosition.getX() + referencePosition.getWidth());
	        	break;
	        case SOUTH:
	        	point.y = (int) (referencePosition.getY() + referencePosition.getHeight() + yInterval);
	        	point.x = (int) referencePosition.getX();
	        	break;
	        case SOUTH_WITHOUT_INTERVAL:
	        	point.y = (int) (referencePosition.getY() + referencePosition.getHeight());
	        	point.x = (int) referencePosition.getX();
	        	break;
	        case NORTHEAST:
	        	point.y = (int) referencePosition.getY() - (height + yInterval);
	        	point.x = (int) (xInterval - referencePosition.getX() + referencePosition.getWidth() - width);
		        break;
	        case NORTHEAST_WITHOUT_INTERVAL:
	        	point.y = (int) referencePosition.getY() - height;
	        	point.x = (int) (referencePosition.getX() + referencePosition.getWidth() - width);
		        break;
        }
	    return point;
    }
	
}
